module.exports = app =>{
  const express = require("express")
  const jwt = require('jsonwebtoken')
  const AdminUser = require('../../models/AdminUser')
  const CustomUser = require('../../models/CustomUser')
  const ShowData = require('../../models/ShowData')
  const ShowMoney = require('../../models/ShowMoney')
  
  const assert = require('http-assert')
  const router = express.Router({
    mergeParams:true
  })
  //创建资源
  router.post('/',async(req,res)=>{
    
    const model = await req.Model.create(req.body)
    res.send(model)
  })
  //更新资源
  router.put('/:id',async(req,res)=>{
      const model = await req.Model.findByIdAndUpdate(req.params.id,req.body)
      res.send(model)
    })
  //删除资源
    router.delete('/:id',async(req,res)=>{
      await req.Model.findByIdAndDelete(req.params.id,req.body)
      res.send({
          success:true
      })
    })
  //资源列表
  router.get('/',async(req,res)=>{
      const items = await req.Model.find()
      res.send(items)
    })
  //资源详情
  router.get('/:id',async(req,res)=>{
      const model = await req.Model.findById(req.params.id)
      console.log(model)
      res.send(model)
  }) 
  //获取统计信息
  app.get('/admin/api/show',async(req,res)=>{
    const model = await ShowData.find().limit(13)
    res.send(model)
  })

  app.get('/admin/api/show_money',async(req,res)=>{
    const model = await ShowMoney.find().limit(13)
    console.log(model);
    res.send(model)
  })
  //登录校验中间键
  const authMiddleware = require('../../middleware/auth')
  const resourceMiddleware = require('../../middleware/resource')
  app.use('/admin/api/rest/:resource',authMiddleware(),resourceMiddleware(),router)

  const multer =require('multer')
  const upload = multer({dest:__dirname+'/../../uploads'})
  app.post('/admin/api/upload',authMiddleware(),upload.single('file'),async(req,res)=>{
     const file = req.file
     //file.url='http://localhost:3000/uploads/'+file.filename
     // 生产环境记得改地址
     file.url='http://mjubuscoming.cn/uploads/'+file.filename
     res.send(file)
  })
  //管理员登录
  app.post('/admin/api/login',async(req,res)=>{
   const {username,password} = req.body
   //1.根据用户名找用户
   
   const user = await AdminUser.findOne({username}).select('+password')
   assert(user,422,'用户不存在')
   //2.校验密码
   const isValid = require('bcrypt').compareSync(password,user.password)
   assert(isValid,422,'密码错误')
   //3.返回token
   const token = jwt.sign({id:user._id},app.get('secret'))
   res.send({token})
  })

  //用户登录
  app.post('/admin/api/c_login',async(req,res)=>{
    const {username,password} = req.body
    //1.根据用户名找用户
    
    const user = await CustomUser.findOne({username}).select('+password')
    assert(user,422,'用户不存在?')
    //2.校验密码
    const isValid = require('bcrypt').compareSync(password,user.password)
    assert(isValid,422,'密码错误')
    //3.返回token
    const token = jwt.sign({id:user._id},app.get('secret'))
    res.send({token})
   })
//错误处理
  app.use(async(err,req,res,next)=>{
    res.status(err.statusCode||500).send({
      message:err.message
    })
  })
}